<template>
  <view v-if="show" class="custom-dialog-mask">
    <view class="custom-dialog-box">
      <image :src="crystalIcon" class="custom-dialog-crystal" />
      <view class="custom-dialog-content">
        <text class="custom-dialog-title">{{ title }}</text>
        <text class="custom-dialog-desc">{{ description }}</text>
        <button class="custom-dialog-confirm" @click="onConfirm(action)">
          {{ confirmText }}
        </button>
      </view>
      <view class="custom-dialog-close-wrapper">
        <img
          @click="onClose"
          class="custom-dialog-close"
          src="/static/close222.png"
          alt=""
        />
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "SubscriptionDialog",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    showCustom: {
      type: Object,
      default: {
        title: "订阅需要话费9,999Y币/月",
        description: "要订阅此智能体吗？",
        confirmText: "确认订阅",
        action: "goToSubscription",
      },
    },
    crystalIcon: {
      type: String,
      default: "/static/88.png", // 请确保有这个水晶球图片
    },
  },
  data() {
    return {
      title: "订阅需要话费9,999Y币/月",
      description: "要订阅此智能体吗？",
      confirmText: "确认订阅",
      action: "goToSubscription",
    }
  },
  watch: {
    showCustom: {
      handler(val) {
        console.log(val)
        if (val) {
          this.$nextTick(() => {
            this.title = val.title
            this.description = val.description
            this.confirmText = val.confirmText
            this.action = val.action
          })
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    // 处理点击事件的统一方法
    onConfirm(action) {
      if (action && typeof this[action] === "function") {
        // 调用指定的方法，并传递参数
        this[action](...[])
      } else {
        console.error(`Method ${action} not found or not a function`)
      }
    },
    // 开始畅聊方法
    upgradeMembership() {
      // 实现升级逻辑
      this.$emit("upgrade", "subscribe")
    },

    // 确认订阅
    goToSubscription() {
      // 这里写订阅逻辑
      this.$emit("subscribe", "vip")
    },
    onClose() {
      this.$emit("close")
    },
  },
}
</script>

<style lang="scss" scoped>
.custom-dialog-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.custom-dialog-box {
  position: relative;
  width: 492rpx;
  height: 383rpx;
  background: #ffffff;
  border-radius: 25rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}

.custom-dialog-crystal {
  position: absolute;
  top: -80rpx;
  width: 130.77rpx;
  height: 236.54rpx;
  z-index: 2;
}

.custom-dialog-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.custom-dialog-title {
  color: #5e6ab5;
  font-size: 31rpx;
  font-weight: 600;
  text-align: center;
  margin-top: 120rpx;
}

.custom-dialog-desc {
  color: #999;
  font-size: 28rpx;
  margin-bottom: 35rpx;
  text-align: center;
}

.custom-dialog-confirm {
  width: 240rpx;
  height: 80rpx;
  background: #6c7deb;
  color: #fff;
  font-size: 31rpx;
  border-radius: 45rpx;
  border: none;
  font-weight: 500;
  margin-top: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.custom-dialog-close-wrapper {
  position: absolute;
  bottom: -140rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.custom-dialog-close {
  width: 55rpx;
  height: 55rpx;
  border-radius: 50%;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.2);
}

.close-icon {
  font-size: 50rpx;
  color: #333;
  font-weight: bold;
  line-height: 1;
}
</style>
